package com.ruoyi.commodity.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.Color;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.commodity.mapper.ColorMapper;
import com.ruoyi.commodity.service.IColorService;

/**
 * 分类管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-09-15
 */
@Service
public class ColorServiceImpl implements IColorService 
{
    @Autowired
    private ColorMapper colorMapper;

    /**
     * 查询分类管理
     *
     * @param id 分类管理主键
     * @return 分类管理
     */
    @Override
    public Color selectColorById(Long id)
    {
        return colorMapper.selectColorById(id);
    }

    /**
     * 查询分类管理列表
     * @param color
     * @return 分类管理
     */
    @Override
    @DataScope(colorAlias = "c")
    public List<Color> selectColorList(Color color)
    {
        return colorMapper.selectColorList(color);
    }

    @Override
    public List<TreeSelect> selectColorTree(Color color) {
        List<Color> colorList = SpringUtils.getAopProxy(this).selectColorList(color);
        return buildColorTreeSelect(colorList);
    }

    @Override
    public List<Color> buildColorTree(List<Color> colorList) {
        List<Color> returnList = new ArrayList<>();
        List<Long> collect = colorList.stream().map(Color::getId).collect(Collectors.toList());
        for (Color color: colorList){
            //如果是顶节点，遍历该父节点下的所以子节的
            if(!collect.contains(color.getParentId())){
                recursionFn(colorList,color);
                returnList.add(color);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = colorList;
        }
        return returnList;
    }

    @Override
    public List<TreeSelect> buildColorTreeSelect(List<Color> colorList) {
        List<Color> colorTree = buildColorTree(colorList);
        return colorTree.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    private void recursionFn(List<Color> colorList , Color color){
        // 得到子节点列表
        List<Color> childList = getChildList(colorList,color);
        color.setChildrenColor(childList);
        for (Color newColor : childList)
        {
            if (hasChild(childList, newColor))
            {
                recursionFn(colorList, newColor);
            }
        }
    }

    /**
     * 获得子节点
     * @param colorList
     * @param color
     * @return
     */
    private List<Color> getChildList(List<Color> colorList, Color color){
        List<Color> returnList = new ArrayList<>();
        Iterator<Color> iterator = colorList.iterator();
        while (iterator.hasNext()){
            Color n = (Color) iterator.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == color.getId().longValue())
            {
                returnList.add(n);
            }
        }
        return returnList;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<Color> colorList, Color color)
    {
        return getChildList(colorList, color).size() > 0;
    }
}
